<?php
  require_once '../classes/ConnectionDB.php';
  require_once '../classes/UtenteManager.php';
  require_once '../classes/TestDataManager.php';
  require_once '../classes/LoadProperties.php';
  require_once '../bean/Argomento.php';

  $properties = new LoadProperties();

  $properties->initServices();
  $connection = ConnectionDB::getInstance()->connectToDB();
  $codiceUtente = $_COOKIE['codiceUtente'];
  $result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

  $argomenti = TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);

  $lastQuestionCode = TestDataManager::getInstance()->getLastQuestionCode($connection);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Nuova domanda</title>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
      <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
      <style type="text/css">
        @import url("../css/style.css");
        #divVeroFalso,#divRispMultipla
        {
          padding-top: 5%;
        }
        #tableRes
        {
          padding-top: 8%;
        }
        a
        {
          text-decoration: none;
          font-size: 15px;
        }

        #listaDomandeBoxCont
        {
          padding-top: 5px;
          padding-left: 3px;
        }

        #headerBox
        {
          padding-top: 20;  
        }


      </style>
      <script type="text/javascript">
        path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
        var rimanenti = 1024;
        window.onload = function(){
          var headerBox = new GlassBox();
          headerBox.init( 'headerBox', '590px', '250px', 'hidden','', true, false);
          headerBox.ipos();
          headerBox.appear();
          var bodyBox = new GlassBox();
          bodyBox.init( 'bodyBox', '920px', '520px', 'hidden','', true, false);
          bodyBox.ipos();
          bodyBox.appear();
          var listaDomandeBox = new GlassBox();
          listaDomandeBox.init( 'listaDomandeBox', '250px', '310px', 'hidden','',true, false);
          listaDomandeBox.apos( '1015px', '40px' );

          listaDomandeBox.appear();

        }


        function loadDomanda()
        {
          var tipologia = document.getElementById('tipologia').value;
          if(tipologia==1)
          {
            document.getElementById('divVeroFalso').style.display="block";
            document.getElementById('divRispMultipla').style.display="none";
          }
          else if(tipologia==2)
          {
            document.getElementById('divVeroFalso').style.display="none";
            document.getElementById('divRispMultipla').style.display="block";
          }

        }

        function salva(tipologia)
        {
          var strError="";
          var codiceArgomento = document.getElementById('codiceArgomento').value;
          var tipologia = document.getElementById('tipologia').value;

          if(tipologia == '1')
          {
            var punteggio = document.getElementById('punteggioVF').value;
            var domanda = document.getElementById('domandaVF').value;

            var rispCorretta;

            if(document.getElementById('vero').checked)
              rispCorretta = "V";
            else
              rispCorretta = "F";

            if(domanda.length==0)
            {
              strError +="10*";
            }
            var rispCorretta="";
            if(document.getElementById('vero').checked)
              rispCorretta = "V";
            if(document.getElementById('falso').checked)
              rispCorretta = "F";
            if(rispCorretta=="")
            {
              strError +="11*";
            }


            if(strError.length>0)
            {
              location.href='../portal/manageErrors.php?lista='+strError;
            }
            else
            {
              document.formVF.codiceArgomento.value = codiceArgomento;
              document.formVF.tipologia.value = tipologia;
              document.formVF.punteggio.value = punteggio;
              document.formVF.domanda.value = domanda;
              document.formVF.rispCorretta.value = rispCorretta;

              document.formVF.submit();
            }

          }

          if(tipologia == '2')
          {
            var punteggio = document.getElementById('punteggioRM').value;
            var domanda = document.getElementById('domandaRM').value;

            if(domanda.length==0)
            {
              strError +="10*";
            }
            if(!document.getElementById('1alternativaCorretta').checked && !document.getElementById('2alternativaCorretta').checked && !document.getElementById('3alternativaCorretta').checked && !document.getElementById('4alternativaCorretta').checked)
            {
              strError += "11*";
            }

            document.formRisMultipla.codiceArgomento.value = codiceArgomento;
            document.formRisMultipla.tipologia.value = tipologia;
            document.formRisMultipla.punteggio.value = punteggio;
            document.formRisMultipla.domanda.value = domanda;

            document.formRisMultipla.primaAlternativa.value = document.getElementById('primaAlternativa').value;
            document.formRisMultipla.secondaAlternativa.value = document.getElementById('secondaAlternativa').value;
            document.formRisMultipla.terzaAlternativa.value = document.getElementById('terzaAlternativa').value;
            document.formRisMultipla.quartaAlternativa.value = document.getElementById('quartaAlternativa').value;
            if(document.formRisMultipla.primaAlternativa.value=="")
              strError +="12*";
            if(document.formRisMultipla.secondaAlternativa.value=="")
              strError +="13*";
            if(document.formRisMultipla.terzaAlternativa.value=="")
              strError +="14*";
            if(document.formRisMultipla.quartaAlternativa.value=="")
              strError +="15*";
            if(strError.length>0)
            {
              location.href='../portal/manageErrors.php?lista='+strError;
            }
            else
            {
              if(document.getElementById('1alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='1';
              if(document.getElementById('2alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='2';
              if(document.getElementById('3alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='3';
              if(document.getElementById('4alternativaCorretta').checked)
                document.formRisMultipla.alternativaCorretta.value='4';
              document.formRisMultipla.submit();
            }

          }

        }

        function checkLenDomanda(domanda)
        {
          var rest;
          max=1024;
          var len = document.getElementById(domanda).value.length;

          if (len > max) {
            document.getElementById(domanda).value = document.getElementById(domanda).value.substring(0,max)
            rest = 0
          }
          else {
            rest = max - len
          }

          document.getElementById('numCaratteriRimanenti').innerHTML = rest;

        }

        function uploadFile()
        {
          var waitBox = new GlassBox();
          waitBox.init( 'waitBox', '920px', '520px', 'hidden','', true, false);
          waitBox.ipos();
          waitBox.appear();
          document.getElementById('formUpload').submit();
          document.getElementById('waitBox').style.display="none";

        }



      </script>
    </head>
    <body>

<?
  if (count($argomenti) == 0)
  {
?>
    <div style="padding-top: 15%; color: white; ">
      <center>
        <h3>Attenzione!!! Per poter inserire una domanda bisogna prima inserire un argomento a cui associare la domada.<br/><br /> <br />
          Per inserirlo clicca <a href="newArgomento.php">qui</a> oppure ritorna al <a href="../portal/newMenu.php">menù</a> principale.</h3>
      </center>
    </div>

<? }
  else
  {
 ?>
    <form enctype="multipart/form-data" name="formVF" method="post" action="newQuestionListener.php">
      <input type="hidden" name="link" value="<? if (isset($_GET["name"]))
    { ?><?= $properties->getUploadPath() ?>/<?= $_GET["name"] ?><? } ?>" />
      <input type="hidden" name="codiceArgomento" />
      <input type="hidden" name="tipologia" />
      <input type="hidden" name="punteggio" />
      <input type="hidden" name="domanda" />
      <input type="hidden" name="rispCorretta" />

    </form>
    <form enctype="multipart/form-data" name="formRisMultipla" method="post" action="newQuestionListener.php">
      <input type="hidden" name="link" value="<? if (isset($_GET["name"]))
    { ?><?= $properties->getUploadPath() ?>/<?= $_GET["name"] ?><? } ?>" />
      <input type="hidden" name="codiceArgomento" />
      <input type="hidden" name="tipologia" />
      <input type="hidden" name="punteggio" />
      <input type="hidden" name="domanda" />
      <input type="hidden" name="primaAlternativa" />
      <input type="hidden" name="secondaAlternativa" />
      <input type="hidden" name="terzaAlternativa" />
      <input type="hidden" name="quartaAlternativa" />
      <input type="hidden" name="alternativaCorretta" />


    </form>
<?php include '../portal/header.php'; ?>
    <center>
      <div id="headerBox">
        <center>
          <table cellpadding="3" style="display: block; padding-left: 55%">
            <tr>
              <td colspan="2">&nbsp;</td>
              <td style="color: #3e7ad4;"><strong>Step 1 - Dettaglio Domanda</strong></td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
              <td style="color: gray;">Step 2 - Upload file</td>
            </tr>

          </table>
          <table cellpadding="13" style="padding-top: 8px;">

            <tr>
              <th>Argomento : </th>
              <td>
                <select name="codiceArgomento" id="codiceArgomento">
                  <?
                    for ($j = 0; $j < count($argomenti); $j++)
                    {
                  ?>
                      <option value="<?= $argomenti[$j]->getCodice() ?>"><?= $argomenti[$j]->getNome() ?></option>
<? } ?>
                  </select>
                </td>
              </tr>
              <tr>
                <th>Tipologia : </th>
                <td>
                  <select name="tipologia" id="tipologia" onchange="javascript:loadDomanda();">
                    <option value="1">Vero / Falso</option>
                    <option value="2">Risposta multipla</option>
                  </select>
                </td>
              </tr>
              <tr>
              <?
                    if (isset($_GET["uploaded"]))
                    {
                      $upload_dir = $_SERVER["DOCUMENT_ROOT"] . "school/uploaded";
              ?>


                      <th>File allegato : </th><td><a href="<?= $properties->getUploadPath() ?>/<?= $_GET["name"] ?>"><?= $_GET["name"] ?></a></td>
<? } ?>

                  </tr>
                </table>
              </center>

            </div>
          </center>
          <center>

            <h3><div style="color: white"><b>Caratteri rimanenti :</b></div>&nbsp;<div style="color: white" id="numCaratteriRimanenti"><script>document.write(rimanenti);</script></div></h3>

            <div id="bodyBox">
              <div id="divVeroFalso" >
                <table width="100%">

                  <tr>

                    <th>Punteggio : </th>

                    <td><select name="punteggioVF" id="punteggioVF">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>

                      </select>

                    </td>

                  </tr>
                  <tr>
                    <th>Domanda : </th>
                    <td>
                      <textarea onkeyup="javascript:checkLenDomanda('domandaVF');" cols="90" rows="8" name="domandaVF" id="domandaVF" ></textarea>
                    </td>
                  </tr>

                </table>
                <br/><br/><br/>
                <table width="100%">
                  <tr>
                    <th>Risposta corretta : </th>

                  </tr>
                  <tr>

                  <table>

                    <tr><td>Vero</td><td><input type="checkbox" name="vero" id="vero" onclick="document.getElementById('falso').checked=false;"/></td></tr>
                    <tr><td>Falso</td><td><input type="checkbox" name="falso" id="falso" onclick="document.getElementById('vero').checked=false;"/></td></tr>


                  </table>

                  </tr>



                </table>
                <input type="button" value="Salva e continua" onclick="javascript:salva('1')"/>

              </div>
              <div id="divRispMultipla" style="display: none">
                <table width="100%">
                  <tr>

                    <th>Punteggio : </th>

                    <td><select name="punteggioRM" id="punteggioRM">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>

                      </select>
                    </td>

                  </tr>
                  <tr>
                    <th>Domanda : </th>
                    <td>
                      <textarea onkeyup="javascript:checkLenDomanda('domandaRM');" cols="90" rows="8" name="domandaRM" id="domandaRM" ></textarea>
                    </td>
                  </tr>

                </table>
                <br/><br/><br/>
                <table>
                  <tr>
                    <th>1° Alternativa</th>
                    <td><input type="text" id="primaAlternativa" name="primaAlternativa" size="50" /></td>
                    <td>Corretta: <input type="checkbox" id="1alternativaCorretta" name="alternativaCorretta"
                                         onclick="document.getElementById('2alternativaCorretta').checked=false;
                                                         document.getElementById('3alternativaCorretta').checked=false;
                                                         document.getElementById('4alternativaCorretta').checked=false;"
                                         value="1" /></td>
                  </tr>
                  <tr>
                    <th>2° Alternativa</th>
                    <td><input type="text" id="secondaAlternativa" name="secondaAlternativa" size="50" /></td>
                    <td>Corretta: <input type="checkbox" id="2alternativaCorretta" name="alternativaCorretta"
                                         onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                         document.getElementById('3alternativaCorretta').checked=false;
                                                         document.getElementById('4alternativaCorretta').checked=false;"
                                         value="2" /></td>
                  </tr>
                  <tr>
                    <th>3° Alternativa</th>
                    <td><input type="text" id="terzaAlternativa" name="terzaAlternativa" size="50" /></td>
                    <td>Corretta: <input type="checkbox" id="3alternativaCorretta" name="alternativaCorretta"
                                         onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                         document.getElementById('2alternativaCorretta').checked=false;
                                                         document.getElementById('4alternativaCorretta').checked=false;"
                                         value="3" /></td>
                  </tr>
                  <tr>
                    <th>4° Alternativa</th>
                    <td><input type="text" id="quartaAlternativa" name="quartaAlternativa" size="50" /></td>
                    <td>Corretta: <input type="checkbox" id="4alternativaCorretta" name="alternativaCorretta"
                                         onclick="document.getElementById('1alternativaCorretta').checked=false;
                                                         document.getElementById('2alternativaCorretta').checked=false;
                                                         document.getElementById('3alternativaCorretta').checked=false;"
                                         value="4" /></td>
                  </tr>


                </table>
                <input type="button" value="Salva e continua" onclick="salva('2')"/>
              </div>

            </div>
          </center>
<? } ?>
  </body>
</html>
